package com.qianfeng.smartdevices.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.smartdevices.cache.CategoriesCache;
import com.qianfeng.smartdevices.dto.ResultCode;
import com.qianfeng.smartdevices.event.UpdateAreaCacheEvent;
import com.qianfeng.smartdevices.exceptions.AddErrorException;
import com.qianfeng.smartdevices.exceptions.DeleteErrorException;
import com.qianfeng.smartdevices.exceptions.UpdateErrorException;
import com.qianfeng.smartdevices.mapper.CategoriesMapper;
import com.qianfeng.smartdevices.pojo.Categories;
import com.qianfeng.smartdevices.pojo.CheckStatus;
import com.qianfeng.smartdevices.service.CategoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Transactional
public class CategoriesServiceImpl implements CategoriesService {

    private CategoriesMapper categoriesMapper;

    private CategoriesCache categoriesCache;

    private ApplicationContext context;
@Autowired
    public void setCategoriesCache(CategoriesCache categoriesCache) {
        this.categoriesCache = categoriesCache;
    }
    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Autowired
    public void setCategoriesMapper(CategoriesMapper categoriesMapper) {
        this.categoriesMapper = categoriesMapper;
    }

    @Override
    public void addCategories(Categories categories) throws Exception {

        boolean isNull = categories.isNull(CheckStatus.ADD);
        if (isNull) {
            //数据不符合我们的要求,还有必要继续执行吗?返回结果给前端
            throw new AddErrorException("数据不完整,请检查数据", ResultCode.DATA_NOT_ALLOW_NUL);
        }
        categoriesMapper.addCategories(categories);
        context.publishEvent(new UpdateAreaCacheEvent());
    }

    @Override
    public PageInfo<Categories> findCategoriesByPage(int pageNum, int pageSize,String categoryname,Long status) {
        PageHelper.startPage(pageNum,pageSize);
        List<Categories> categoriesList = categoriesCache.getAllData();//从缓存中获取数据
        List<Categories> subList = categoriesList.stream().filter(categories -> {
            boolean b1 = ObjectUtils.isEmpty(categoryname) ? true : categories.getCategoryname().contains(categoryname);
            boolean b2 = (ObjectUtils.isEmpty(status) || status == -100) ? true : categories.getStatus().equals(status);

            return b1 && b2 ;
        }).collect(Collectors.toList());
        List<Categories> list = subList.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        PageInfo<Categories> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(subList.size());
        return pageInfo;
    }

    @Override
    public void deleteById(Long[] ids) {
        if (ids == null || ids.length == 0) {
            throw new DeleteErrorException("删除需要传递ids",ResultCode.DATA_NOT_ALLOW_NUL);
        }
        categoriesMapper.deleteById(Arrays.asList(ids));
        context.publishEvent(new UpdateAreaCacheEvent());
    }


    @Override
    public void updateCategories(Categories categories)  {
        boolean isNull = categories.isNull(CheckStatus.UPDATE);
        if (isNull) {
            //数据不符合我们的要求,还有必要继续执行吗?返回结果给前端
            throw new UpdateErrorException("更新的数据为空", ResultCode.DATA_NOT_ALLOW_NUL);
        }

        categoriesMapper.updateCategories(categories);
        context.publishEvent(new UpdateAreaCacheEvent());
    }


}
